package com.ww.jsoup;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @author justLym
 * @version 1.0.0 2020/4/23 19:44
 **/
public class JsoupMain {
    public static void main(String[] args) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet0");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            int rowIndex = 0;
            for (int i = 1; i <= 39; i++) {
                String url = "http://gtpw.zowoyoo.com:38087/api/product/search?treeId=0&areaId=1004201&keyword=&page="+i+"&resultNum=10&sort=1";
                Connection conn = Jsoup.connect(url).timeout(5000);
                conn.header("Accept", "*/*");
                conn.header("Content-Type", "application/json;charset=UTF-8");
                conn.header("Accept-Encoding", "gzip, deflate");
                conn.header("Accept-Language", "zh-CN,zh;q=0.9");
                conn.header("X-Authorization", "Bearer eyJhbGciOiJIUzUxMiIsInppcCI6IkRFRiJ9.eNoki8EOgjAQBf_lnXvYrWCh3-HNeGhNEzSRbVwbqIR_B8JxJjMLtER4cEedta2GoLYf4rvCQJ-Sk8LfEUYZ60eK4mFQNH1vNafj2quX7gn-MkkVOTj84PlKzaUnJmeQ5nyKhtmRWzcAAAD__w.7baESfcVE1j9mf58XiAggisKxto4Tf7EaucEoEFzr3HsxQumL760jPYiVEO_oDwy_0RYuScCvC1gROLfB6DaIQ");
                conn.header("Host", "gtpw.zowoyoo.com:38087");
                conn.header("Origin", "http://gtpw.zowoyoo.com");
                conn.header("Referer", "http://gtpw.zowoyoo.com/");
                conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
                String str = conn.ignoreContentType(true).execute().body();
                JSONObject json = JSONObject.parseObject(str);
                JSONArray results = json.getJSONArray("results");
                System.out.println(">>>" + i);
                String[] indexStr = new String[]{"infoTitle", "mchName", "advanceDay", "priceStartDate", "priceEndDate", "minPrice", "minSalePrice"};
                for (int j = 0; j < results.size(); j++) {
                    JSONObject rowObject = results.getJSONObject(j);
                    HSSFRow row = sheet.createRow(rowIndex);
                    for (int k = 0; k < indexStr.length; k++) {
                        if(k == 2){
                            String advanceDayStr = "当天可预订";
                            int advanceDay = rowObject.getIntValue(indexStr[k]);
                            if(advanceDay == 1){
                                advanceDayStr = "提前"+advanceDay+"天预订";
                            }
                            row.createCell(k).setCellValue(advanceDayStr);
                        }else if (k == 3 || k == 4){
                            row.createCell(k).setCellValue(sdf.format(rowObject.get(indexStr[k])));
                        }else {
                            row.createCell(k).setCellValue(rowObject.get(indexStr[k]).toString());
                        }
                    }
                    rowIndex++;

                    /*FileUtils.appendContent2File("/Users/wangwei/soft/zwy.txt", results.getJSONObject(j).get("infoTitle").toString() + "\r\n");
                    FileUtils.appendContent2File("/Users/wangwei/soft/zwy.txt", results.getJSONObject(j).get("mchName").toString() + "\r\n");
                    String advanceDayStr = "当天可预订";
                    int advanceDay = results.getJSONObject(j).getIntValue("advanceDay");
                    if(advanceDay == 1){
                        advanceDayStr = "提前"+advanceDay+"天预订";
                    }
                    FileUtils.appendContent2File("/Users/wangwei/soft/zwy.txt", advanceDayStr + "\r\n");
                    FileUtils.appendContent2File("/Users/wangwei/soft/zwy.txt", sdf.format(results.getJSONObject(j).get("priceStartDate")) + "\r\n");
                    FileUtils.appendContent2File("/Users/wangwei/soft/zwy.txt", sdf.format(results.getJSONObject(j).get("priceEndDate")) + "\r\n");
                    FileUtils.appendContent2File("/Users/wangwei/soft/zwy.txt", "结算价:" + results.getJSONObject(j).get("minPrice").toString() + "\r\n");
                    FileUtils.appendContent2File("/Users/wangwei/soft/zwy.txt", "销售价:" + results.getJSONObject(j).get("minSalePrice").toString() + "\r\n");
                    FileUtils.appendContent2File("/Users/wangwei/soft/zwy.txt", "\r\n");*/

//                    System.out.println(results.getJSONObject(j).get("infoTitle"));
//                    System.out.println(results.getJSONObject(j).get("mchName"));
//                    System.out.println(sdf.format(results.getJSONObject(j).get("priceStartDate")));
//                    System.out.println(sdf.format(results.getJSONObject(j).get("priceEndDate")));
//                    System.out.println(results.getJSONObject(j).get("minPrice"));
                }
            }

            FileOutputStream fos = new FileOutputStream("/Users/wangwei/soft/ww.xls");
            wb.write(fos);
            fos.flush();
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}